﻿namespace WMS6.Web.Configuration
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoServiceRef;

    public class Carton : BasePage
    {
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected CheckBox chbIsActive;
        protected CheckBox chbSysteCreate;
        protected HtmlForm F2111;
        protected HtmlInputHidden hdnFrom;
        protected HtmlImage imgCartonGroup;
        protected Label lblCartonGroup;
        protected Label lblCartonID;
        protected Label lblCartonType;
        protected Label lblCube;
        protected Label lblDesc;
        protected Label lblHeight;
        protected Label lblLength;
        protected Label lblMaxCount;
        protected Label lblMaxWeight;
        protected Label lblMsg;
        protected Label lblOwner;
        protected Label lblSeqNo;
        protected Label lblWidth;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        protected TextBox txtCartonGroup;
        protected TextBox txtCartonID;
        protected TextBox txtCartonType;
        protected TextBox txtCube;
        protected TextBox txtDesc;
        protected TextBox txtHeight;
        protected TextBox txtLength;
        protected TextBox txtMaxCount;
        protected TextBox txtMaxWeight;
        protected TextBox txtSeqNo;
        protected TextBox txtWidth;
        protected UpdatePanel up1;

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            this.txtCartonID.Text = "";
            this.txtCartonID.Enabled = false;
            this.txtCartonGroup.Text = "";
            this.txtSeqNo.Text = "";
            this.chbSysteCreate.Checked = false;
            base.DataEntityViewState = null;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.chbSysteCreate.Checked)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("C011");
                }
                else if (this.txtCartonGroup.Text.Trim() != "")
                {
                    List<string> cartonID = new List<string> {
                        this.txtCartonID.Text.Trim()
                    };
                    this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Delete(cartonID, "CARTON") + base.GetGlobalMsgResource("C006");
                    this.btnSave.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnCopy.Enabled = false;
                    base.DataEntityViewState = null;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C007") + ex.Message;
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("CartonQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("CartonList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.txtCartonGroup.Text.Trim() == "")
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2260_001");
            }
            else if (base.DataEntityViewState != null)
            {
                this.UpdateCartonInfo();
            }
            else
            {
                this.InsertCartonInfo();
            }
        }

        private void InsertCartonInfo()
        {
            try
            {
                decimal outDecimal = 0.0M;
                int outInt = 0;
                CartonInfo cartonInfo = new CartonInfo {
                    Description = this.txtDesc.Text.Trim(),
                    CartonGroup = this.txtCartonGroup.Text.Trim()
                };
                if (this.txtLength.Text.Trim() != "")
                {
                    if (decimal.TryParse(this.txtLength.Text.Trim(), out outDecimal))
                    {
                        cartonInfo.Length = decimal.Parse(this.txtLength.Text.Trim());
                    }
                    else
                    {
                        this.lblMsg.Text = base.GetGlobalMsgResource("F2260_004");
                        return;
                    }
                }
                if (this.txtCube.Text.Trim() != "")
                {
                    if (decimal.TryParse(this.txtCube.Text.Trim(), out outDecimal))
                    {
                        cartonInfo.Cube = decimal.Parse(this.txtCube.Text.Trim());
                    }
                    else
                    {
                        this.lblMsg.Text = base.GetGlobalMsgResource("F2260_003");
                        return;
                    }
                }
                cartonInfo.CartonType = this.txtCartonType.Text.Trim();
                if (this.txtWidth.Text.Trim() != "")
                {
                    if (decimal.TryParse(this.txtWidth.Text.Trim(), out outDecimal))
                    {
                        cartonInfo.Width = decimal.Parse(this.txtWidth.Text.Trim());
                    }
                    else
                    {
                        this.lblMsg.Text = base.GetGlobalMsgResource("F2260_002");
                        return;
                    }
                }
                if (this.txtMaxWeight.Text.Trim() != "")
                {
                    if (decimal.TryParse(this.txtMaxWeight.Text.Trim(), out outDecimal))
                    {
                        cartonInfo.MaxWeight = decimal.Parse(this.txtMaxWeight.Text.Trim());
                    }
                    else
                    {
                        this.lblMsg.Text = base.GetGlobalMsgResource("F2260_005");
                        return;
                    }
                }
                if (this.txtSeqNo.Text.Trim() != "")
                {
                    if (int.TryParse(this.txtSeqNo.Text.Trim(), out outInt))
                    {
                        cartonInfo.SeqNo = int.Parse(this.txtSeqNo.Text.Trim());
                    }
                    else
                    {
                        this.lblMsg.Text = base.GetGlobalMsgResource("F2260_006");
                        return;
                    }
                }
                if (this.txtHeight.Text.Trim() != "")
                {
                    if (decimal.TryParse(this.txtHeight.Text.Trim(), out outDecimal))
                    {
                        cartonInfo.Height = decimal.Parse(this.txtHeight.Text.Trim());
                    }
                    else
                    {
                        this.lblMsg.Text = base.GetGlobalMsgResource("F2260_007");
                        return;
                    }
                }
                if (this.txtMaxCount.Text.Trim() != "")
                {
                    if (decimal.TryParse(this.txtMaxCount.Text.Trim(), out outDecimal))
                    {
                        cartonInfo.MaxCount = decimal.Parse(this.txtMaxCount.Text.Trim());
                    }
                    else
                    {
                        this.lblMsg.Text = base.GetGlobalMsgResource("F2260_008");
                        return;
                    }
                }
                cartonInfo.IsActive = this.chbIsActive.Checked ? "Y" : "N";
                cartonInfo.SystemCreated = this.chbSysteCreate.Checked ? "Y" : "N";
                cartonInfo.CreatedBy = base.UserInfo.UserID;
                cartonInfo.UpdatedBy = base.UserInfo.UserID;
                string cartonID = BaseInfoServiceClientProxy.GetInstance().Insert(cartonInfo, "CARTON");
                this.lblMsg.Text = cartonID + base.GetGlobalMsgResource("C004");
                this.txtCartonID.Text = cartonID;
                base.DataEntityViewState = cartonInfo;
                this.imgCartonGroup.Visible = false;
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C005") + ex.Message;
            }
        }

        private void LoadData()
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.SetCartonInfo2Form();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetCartonInfo2Form()
        {
            try
            {
                string cattonID = base.Request.QueryString["ID"].ToString();
                CartonInfo cartonInfo = BaseInfoServiceClientProxy.GetInstance().GetInfoByID(cattonID, "CARTON") as CartonInfo;
                base.DataEntityViewState = cartonInfo;
                if (cartonInfo != null)
                {
                    this.txtCartonID.Text = cartonInfo.CartonID;
                    this.txtDesc.Text = cartonInfo.Description;
                    this.txtCartonGroup.Text = cartonInfo.CartonGroup;
                    this.txtLength.Text = base.GetPreferenceDigit(cartonInfo.Length);
                    this.txtCube.Text = base.GetPreferenceDigit(cartonInfo.Cube);
                    this.txtCartonType.Text = cartonInfo.CartonType;
                    this.txtWidth.Text = base.GetPreferenceDigit(cartonInfo.Width);
                    this.txtMaxWeight.Text = base.GetPreferenceDigit(cartonInfo.MaxWeight);
                    this.txtSeqNo.Text = cartonInfo.SeqNo.ToString();
                    this.txtHeight.Text = base.GetPreferenceDigit(cartonInfo.Height);
                    this.txtMaxCount.Text = base.GetPreferenceDigit(cartonInfo.MaxCount);
                    this.chbIsActive.Checked = cartonInfo.IsActive == "Y";
                    this.chbSysteCreate.Checked = cartonInfo.SystemCreated == "Y";
                    this.txtCartonID.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void UpdateCartonInfo()
        {
            try
            {
                decimal outDecimal = 0.0M;
                int outInt = 0;
                CartonInfo cartonInfo = base.DataEntityViewState as CartonInfo;
                cartonInfo.CartonID = this.txtCartonID.Text.Trim();
                cartonInfo.Description = this.txtDesc.Text.Trim();
                cartonInfo.CartonGroup = this.txtCartonGroup.Text.Trim();
                if (decimal.TryParse(this.txtWidth.Text.Trim(), out outDecimal))
                {
                    cartonInfo.Width = decimal.Parse(this.txtWidth.Text.Trim());
                }
                else
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F2260_002");
                    return;
                }
                if (decimal.TryParse(this.txtCube.Text.Trim(), out outDecimal))
                {
                    cartonInfo.Cube = decimal.Parse(this.txtCube.Text.Trim());
                }
                else
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F2260_003");
                    return;
                }
                cartonInfo.CartonType = this.txtCartonType.Text.Trim();
                if (decimal.TryParse(this.txtLength.Text.Trim(), out outDecimal))
                {
                    cartonInfo.Length = decimal.Parse(this.txtLength.Text.Trim());
                }
                else
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F2260_004");
                    return;
                }
                if (decimal.TryParse(this.txtMaxWeight.Text.Trim(), out outDecimal))
                {
                    cartonInfo.MaxWeight = decimal.Parse(this.txtMaxWeight.Text.Trim());
                }
                else
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F2260_005");
                    return;
                }
                if (int.TryParse(this.txtSeqNo.Text.Trim(), out outInt))
                {
                    cartonInfo.SeqNo = int.Parse(this.txtSeqNo.Text.Trim());
                }
                else
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F2260_006");
                    return;
                }
                if (decimal.TryParse(this.txtHeight.Text.Trim(), out outDecimal))
                {
                    cartonInfo.Height = decimal.Parse(this.txtHeight.Text.Trim());
                }
                else
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F2260_007");
                    return;
                }
                if (decimal.TryParse(this.txtMaxCount.Text.Trim(), out outDecimal))
                {
                    cartonInfo.MaxCount = decimal.Parse(this.txtMaxCount.Text.Trim());
                }
                else
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F2260_008");
                    return;
                }
                cartonInfo.IsActive = this.chbIsActive.Checked ? "Y" : "N";
                cartonInfo.SystemCreated = this.chbSysteCreate.Checked ? "Y" : "N";
                cartonInfo.CreatedBy = base.UserInfo.UserID;
                cartonInfo.UpdatedBy = base.UserInfo.UserID;
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Update(cartonInfo, "CARTON") + base.GetGlobalMsgResource("C002");
                this.imgCartonGroup.Visible = false;
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C003") + ex.Message;
            }
        }
    }
}

